<?php
use Doctrine\ORM;
require_once realpath(APPLICATION_PATH . '/../library/Doctrine/Common/ClassLoader.php');
use Doctrine\ORM\EntityManager, Doctrine\ORM\Configuration;
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

	static $INI_DEFAULT_FILE = "db.bemigo.shadow.ini";

	protected function _initConfigload(){
		$iniFile = "db.bemigo.ini";

		if(!file_exists(CONFIG_PATH . DIRECTORY_SEPARATOR . $iniFile)){
			$iniFile = self::$INI_DEFAULT_FILE;
		}
		$config = new Zend_Config_Ini(CONFIG_PATH . DIRECTORY_SEPARATOR . $iniFile, APPLICATION_ENV);
		Zend_Registry::set("bemigoDoctrineConfig", $config);

		$config = new Zend_Config_Ini(CONFIG_PATH.'/config.ini', APPLICATION_ENV);
		Zend_Registry::set("configuration", $config);
	}

	protected function _initLog(){
		if($this->hasPluginResource("log")){
			$resource = $this->getPluginResource("log");
			$log = $resource->getLog();
			Zend_Registry::set("log", $log);
		}else{
			throw new Exception("Error setting logger", 0);
		}
	}

	protected function _initActionHelper(){
		Zend_Controller_Action_HelperBroker::addPath(realpath(dirname(__FILE__)) . '/../library/Bemigo/Controller/Action/Helper', 'Bemigo_Controller_Action_Helper');
	}

	protected function _initDoctrine2Em(){

		$bemigoDoctrineConfig = Zend_Registry::get("bemigoDoctrineConfig");

		$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\ORM', realpath(APPLICATION_PATH . "/../library/"));
		$classLoader->register();

		$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\DBAL', realpath(APPLICATION_PATH . "/../library/"));
		$classLoader->register();

		$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\Common', realpath(APPLICATION_PATH . "/../library/"));
		$classLoader->register();

		$classLoader = new \Doctrine\Common\ClassLoader('Entities', realpath(APPLICATION_PATH));
		$classLoader->register();

		$classLoader = new \Doctrine\Common\ClassLoader('Proxies', realpath(APPLICATION_PATH));
		$classLoader->register();

		if($bemigoDoctrineConfig->doctrine->cache){
			$cache = new \Doctrine\Common\Cache\ApcCache();
		}else{
			$cache = new \Doctrine\Common\Cache\ArrayCache();
		}
		$config = new Configuration();
		$config->setMetadataCacheImpl($cache);
		$driverImpl = $config->newDefaultAnnotationDriver(APPLICATION_PATH . '/Entities');
		$config->setMetadataDriverImpl($driverImpl);
		$config->setQueryCacheImpl($cache);
		$config->setProxyDir(APPLICATION_PATH . '/Proxies');
		$config->setProxyNamespace('Proxies');

		if($bemigoDoctrineConfig->doctrine->proxy){
			$config->setAutoGenerateProxyClasses(true);
		}else{
			$config->setAutoGenerateProxyClasses(false);
		}

		$connectionOptions = array('dbname'=>$bemigoDoctrineConfig->doctrine->dbname, 'user'=>$bemigoDoctrineConfig->doctrine->user,
			'password'=>$bemigoDoctrineConfig->doctrine->password, 'host'=>$bemigoDoctrineConfig->doctrine->host,
			'driver'=>$bemigoDoctrineConfig->doctrine->driver);
		$em = EntityManager::create($connectionOptions, $config);
		Zend_Registry::set("em", $em);
	}

}

